import 'package:flutter/material.dart';

void moreDialog(BuildContext context) {
  OverlayEntry? entry;

  // findRenderObject获取当前实例的定位
  RenderBox renderBox = context.findRenderObject() as RenderBox;
  // localToGlobal将给入的坐标点转为全局的坐标点
  Offset offset = renderBox.localToGlobal(Offset.zero); //Offset.zero偏移量为0表示当前点
  final height = context.size!.height;

  List items = [
    {"icon": Icons.chat_bubble, "content": "发起群聊"},
    {"icon": Icons.group_add_sharp, "content": "添加朋友"},
    {"icon": Icons.fire_extinguisher_rounded, "content": "扫一扫"},
    {"icon": Icons.qr_code_sharp, "content": "收付款"}
  ];

  entry = OverlayEntry(builder: (context) {
    // Material与MaterialAPP差不多，只是优先级低一些
    return Material(
        // Material默认有个白色的遮罩层，这里进行透明
        type: MaterialType.transparency,
        child: InkWell(
          onTap: () {
            // 移除overlay弹窗
            entry!.remove();
          },
          child: Align(
            alignment: Alignment.topRight,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Container(
                  height: 7,
                  width: 20,
                  margin:
                      EdgeInsets.only(top: offset.dy + height - 10, right: 14),
                  // CustomPaint：自定义画笔
                  child: CustomPaint(
                    // 自定义绘画方式
                    painter: MorePainter(),
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(10),
                  margin: const EdgeInsets.only(right: 10),
                  width: 120,
                  height: 178,
                  decoration: BoxDecoration(
                      color: const Color(0xff4C4C4C),
                      borderRadius: BorderRadius.circular(5)),
                  child: Column(
                      children: items
                          .map((item) => Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Icon(
                                    item["icon"],
                                    color: Colors.white,
                                  ),
                                  Expanded(
                                      child: Container(
                                    alignment: Alignment.center,
                                    padding: EdgeInsets.only(
                                        bottom: item != items.last ? 10 : 0),
                                    margin: EdgeInsets.only(
                                        left: 10,
                                        bottom: item != items.last ? 10 : 0),
                                    decoration: BoxDecoration(
                                        border: Border(
                                            bottom: BorderSide(
                                                color: item != items.last
                                                    ? const Color(0xff535353)
                                                    : Colors
                                                        .transparent))), //0xff535353
                                    child: Text(
                                      item["content"],
                                      style:
                                          const TextStyle(color: Colors.white),
                                    ),
                                  ))
                                ],
                              ))
                          .toList()),
                )
              ],
            ),
          ),
        ));
  });
  // Overlay是一个stack弹窗，将entry插入到overlay中，使独立的child窗口悬浮于其他widget之上
  Overlay.of(context).insert(entry);
}

// 自定义画笔绘画方式
class MorePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    print("这里是否执行");
    Paint paint = Paint()
      ..color = const Color(0xff4C4C4C)
      ..style = PaintingStyle.fill; //画笔

    Path path = Path();
    path.moveTo(0, size.height);
    path.lineTo(size.width / 2 - 3, 0);
    path.lineTo(size.width / 2 + 3, 0);
    path.lineTo(size.width, size.height);
    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(MorePainter oldDelegate) => false;
}
